import { _decorator, Component, Node, director, tween, v3, Toggle, Label, EventTouch, Prefab, Sprite, Widget } from 'cc';
import { DataManager } from '../Public/DataManager';
import { PoolManager } from '../Public/PoolManager';
import { PublicFn } from '../Public/PublicFn';
const { ccclass, property } = _decorator;

@ccclass('Thread')
export class Thread extends Component {
    /**关卡页面切换 */
    @property([Node])
    pageArr: Node[] = [];
    @property(Node)
    toggle: Node = null;
    @property(Prefab)
    tog: Prefab = null;
    @property(Widget)
    top:Widget=null;
    curPage: number = 1;
    maxPage: number = 1;
    isMove: boolean = false;
    start() {
            this.initData();
        // console.log('wx.getSystemInfoSync().safeArea :>> ', wx.getSystemInfoSync().safeArea);
        try {
            this.top.top=wx.getSystemInfoSync().safeArea.top;
        } catch (error) {
            console.log('error',error)
        }
        
    }

    update(deltaTime: number) {

    }
    /**返回主页 */
    BacktoMain() {
        this.node.setPosition(1000, 0);
    }
    /**进行游玩 */
    PlayGame(e: EventTouch) {
        if (e.target['act']) {
            DataManager.Instance.curLevel = e.target.getSiblingIndex();
            director.loadScene(`PlayScene`);
        }
        else {
            console.log('no :>> ');
        }
    }
    /**下一页 */
    NextPage() {
        if (this.isMove)
            return;
        if (this.curPage < this.maxPage) {
            this.isMove = true;
            const { LevelArr,nodeLens } = DataManager.Instance
            const len = nodeLens;
            const arr = LevelArr.slice(this.curPage * 20, this.curPage * 20+20);
            const self = this;
            const node = this.pageArr.shift();
            // console.log('arr :>> ', arr);
            // console.log('LevelArr :>> ', LevelArr);
            this.pageArr.push(node);
            tween().parallel(
                tween(this.pageArr[0]).by(0.7, { position: v3(-320) }, { onComplete() { self.isMove = false } }).start(),
                tween(this.pageArr[1]).by(0.7, { position: v3(-320) }).start(),
            ).start();
            this.pageArr[2].setPosition(v3(320, 175));
            arr.forEach((e, i) => {
                this.pageArr[1].children[i].getComponentInChildren(Label).string = `${i + 1+(this.curPage * 20)}`;
                this.pageArr[1].children[i].active = true;
            });
            console.log('len :>> ', len);
            if (len >= (this.curPage) * 20 && len < (this.curPage) * 20 + 20) {
                PublicFn.Instance.changeImg(`imgs/胡萝卜关`, this.pageArr[1].children[len%20].getComponent((Sprite)));
                this.pageArr[1].children[len%20]['act'] = true;
            }
            this.toggle.children[++this.curPage - 1].getComponent(Toggle).isChecked = true;
        }
    }
    /**上一页 */
    prePage() {
        if (this.isMove)
            return;
        if (this.curPage > 1) {
            const { LevelArr, nodeLens } = DataManager.Instance
            const len = nodeLens;
            const arr = LevelArr.slice((this.curPage - 2) * 20, (this.curPage - 2) * 20+20);
            this.isMove = true;
            const self = this;
            const node = this.pageArr.pop();
            this.pageArr.unshift(node);
            tween().parallel(
                tween(this.pageArr[1]).by(0.7, { position: v3(320) }, { onComplete() { self.isMove = false } }).start(),
                tween(this.pageArr[2]).by(0.7, { position: v3(320) }).start(),
            ).start();
            this.pageArr[0].setPosition(v3(-320, 175));
            arr.forEach((e, i) => {
                if ((this.curPage - 2) * 20 + i < len) {
                    this.pageArr[1].children[i]['act'] = true;
                    PublicFn.Instance.changeImg(`imgs/选中胡萝卜关`, this.pageArr[1].children[i].getComponent((Sprite)));
                }
                this.pageArr[1].children[i].getComponentInChildren(Label).string = `${i + 1+(this.curPage - 2) * 20}`;
                this.pageArr[1].children[i].active = true;
            });
            if (len >= (this.curPage - 2) * 20 && len < (this.curPage - 2) * 20 + 20) {
                PublicFn.Instance.changeImg(`imgs/胡萝卜关`, this.pageArr[1].children[len%20].getComponent((Sprite)));
                this.pageArr[1].children[len%20]['act'] = true;
            }
            this.toggle.children[--this.curPage - 1].getComponent(Toggle).isChecked = true;

        }
    }
    /**数据初始化 */
    initData() {
        let xhr = new XMLHttpRequest();
        const { LevelArr } = DataManager.Instance
        if (LevelArr.length) {
            this.renderView();
            return;
        }
        xhr.open(`post`, `https://2857e2g653.imdo.co/index.php/tpp/nodeList`);
        xhr.setRequestHeader(`content-type`, `application/x-www-form-urlencoded`);
        xhr.setRequestHeader(`token`, `${DataManager.Instance.token}`);
        xhr.send(`clearance_id=${DataManager.Instance.threadID}`);
        xhr.onreadystatechange = () => {
            if (xhr.readyState === 4 && xhr.status === 200) {
                const { code, data } = JSON.parse(xhr.response)
                if (code == 200) {
                    DataManager.Instance.LevelArr = data;
                    console.log('data :>> ', data);
                    this.renderView();
                }
            }
        }
    }
    /**关卡数量渲染 */
    renderView() {
        const { LevelArr,nodeLens } = DataManager.Instance
        this.maxPage = (LevelArr.length / 20);
        const arr=LevelArr.slice(0, 20);
        arr.forEach((e, i) => {
            if (i < nodeLens) {
                this.pageArr[1].children[i]['act'] = true;
                PublicFn.Instance.changeImg(`imgs/选中胡萝卜关`, this.pageArr[1].children[i].getComponent((Sprite)));
            }
            this.pageArr[1].children[i].getComponentInChildren(Label).string = `${i + 1}`;
            this.pageArr[1].children[i].active = true;
        });
        for (let i = 0; i < this.maxPage; i++) { 
            PoolManager.Instance.getNode(this.tog, this.toggle,`tog`);
        }
        if (nodeLens < 20) {
            PublicFn.Instance.changeImg(`imgs/胡萝卜关`, this.pageArr[1].children[nodeLens].getComponent((Sprite)));
            this.pageArr[1].children[nodeLens]['act'] = true;
        }
    }
}

